<?php
/**
 * Coffee Framework

 * @category   CF
 * @package    CF_db
 * @author     그네 <Jung Sik, Park> <ccooffeee at hotmail dot com>
 * @copyright  Copyright (c) 2009 Jung Sik, Park <ccooffeee at hotmail dot com>
 * @license    doc/LICENSE    Based New BSD License
 * @version    $Id: db.php,v 0.1.3 2010/08/09 ccooffeee Exp $

 * LICENSE

 * 본 프로그램은 New BSD License 를 기본으로 하고 있지만 약간 다른 제약조건을 가지고 있습니다. 
 * 함께 제공된 license file 인  doc/LICENSE 를 꼭 확인하시길 바랍니다.

 * This Source is subject to the New BSD License with SOME CONSTRAINT that is boudled
 * with this package in the file doc/LICENSE 
 */



require_once __DIR__.'/abstract.php';


class CF_db_pgsql extends CF_db_abstract
{
// {{{ properties
	private $savepoint = null;
// }}}


// {{{ void commit ()
	public function commit() {
		parent::commit();

		$this->savepoint = null;
	}
// }}}
// {{{ void rollback ([string savepoint])
	public function rollback ($savepoint = null) {
		if($this->dao === null) return;
		if($this->transaction !== true) return;

		$sp = $this->getSavepoint();

		if($savepoint && ($savepoint == $sp)) {
			$this->exec("ROLLBACK TO $savepoint");
		}
		else {
			parent::rollback();
			$this->savepoint = null;
		}
    }
// }}}
// {{{ final void setSavepoint (string savepoint)
    final public function setSavepoint($pt = null) {
        if($this->transaction !== true) return false;

        $sp = $this->getSavepoint();
        if($sp) $this->exec("RELEASE SAVEPOINT ".$sp);
        if($pt) $this->exec("SAVEPOINT ".$pt);
        $this->savepoint = $pt;
    }
// }}}


// {{{ mixed preparedStatement ([string name])
	public function preparedStatement ($name = null) {
		if($this->dao === null) return array();

		if($name !== null) {
			$name = $this->escape($name);

			$query = "SELECT name FROM pg_prepared_statements WHERE name = '$name'";
			return $this->query($query)->field() ? true : false;
		}
		else {
			$query = "SELECT name FROM pg_prepared_statements";
			return $this->query($query)->fetchAll();
		}
	}
// }}}

// {{{ string escape (string)
	public function escape ($str) {
		if($this->_isQuoted())
			$str = stripslashes($str);

		$str = str_replace("'", "''", $str);
		$str = str_replace("\\", "' || E'\\\\' || '", $str);

        return $str;
	}
// }}}



// {{{ protected void init ()
	protected function init() {
		parent::init();

		if(isset($this->config['encoding']) && $this->config['encoding'])
			$this->exec('SET client_encoding TO '.$this->config['encoding']);
	}
// }}}

// {{{ private string getSavepoint ()
	private function getSavepoint () {
		return $this->savepoint;
	}
// }}}
}

// vim: ts=4 sw=4 fdm=marker
?>
